//
//  ChatVoiceViewCell.m
//  EaseMob
//
//  Created by 王方伟 on 16/7/26.
//  Copyright © 2016年 wfw. All rights reserved.
//

#import "ChatVoiceView.h"
#import "EMMessageFrame.h"
#import "EMVoiceMessageBody.h"
#import "EMCDDeviceManager.h"

@interface ChatVoiceView ()
@property (weak, nonatomic) UIImageView *imageView;
@property (weak, nonatomic) UILabel *label;
@end

static UIImageView *_animatView;
@implementation ChatVoiceView
- (instancetype)init{
    self = [super init];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView{
    self.backgroundColor = [UIColor clearColor];
    UIImageView *imageView = [[UIImageView alloc] init];
    [self addSubview:imageView];
    _imageView = imageView;
    
    UILabel *label = [[UILabel alloc] init];
    [self addSubview:label];
    self.label = label;
    UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playVoice)];
    [self addGestureRecognizer:tapGest];
}

- (void)playVoice{
    [_animatView stopAnimating];
    [_animatView removeFromSuperview];
    EMMessage *message = _frameModel.message;
    EMVoiceMessageBody *body = (EMVoiceMessageBody *)message.body;
    NSString *path = body.localPath;
    NSFileManager *fileM = [NSFileManager defaultManager];
    if (![fileM fileExistsAtPath:path]) {
        path = body.remotePath;
    }
    
    _animatView = [[UIImageView alloc] init];
    _animatView.frame = _imageView.bounds;
    [_imageView addSubview:_animatView];
    
    if (_frameModel.type == MessageTypeMe) {
        _animatView.animationImages = @[[UIImage imageNamed:@"chat_sender_audio_playing_000"],
                                       [UIImage imageNamed:@"chat_sender_audio_playing_001"],
                                       [UIImage imageNamed:@"chat_sender_audio_playing_002"],
                                       [UIImage imageNamed:@"chat_sender_audio_playing_003"]];
    }else{
        _animatView.animationImages = @[[UIImage imageNamed:@"chat_receiver_audio_playing000"],
                                       [UIImage imageNamed:@"chat_receiver_audio_playing001"],
                                       [UIImage imageNamed:@"chat_receiver_audio_playing002"],
                                       [UIImage imageNamed:@"chat_receiver_audio_playing003"]];
    }
    _animatView.animationDuration = 1;
    [_animatView startAnimating];
    [[EMCDDeviceManager sharedInstance] asyncPlayingWithPath:path completion:^(NSError *error) {
        if (!error) {
            [_animatView stopAnimating];
            MyLog(@"声音播放完成");
        }
    }];
}

- (void)setFrameModel:(EMMessageFrame *)frameModel{
    _frameModel = frameModel;
    EMMessage *message = frameModel.message;
    EMVoiceMessageBody *body = (EMVoiceMessageBody *)message.body;
    _label.text = [NSString stringWithFormat:@"%d'", body.duration];
    CGFloat labelH = self.height;
    CGFloat labelW = 60;
    CGFloat imageMargin = 30;
    if (_frameModel.type == MessageTypeMe) {
        _imageView.bounds = CGRectMake(0, 0, 20, 20);
        _imageView.center = CGPointMake(self.width - imageMargin, self.height *0.5);
        _imageView.image = [UIImage imageNamed:@"chat_sender_audio_playing_full"];
        _label.frame = CGRectMake(_imageView.x - 20, 0, labelW, labelH);
    }else{
        _imageView.bounds = CGRectMake(0, 0, 20, 20);
        _imageView.center = CGPointMake(imageMargin, self.height *0.5);
        _imageView.image = [UIImage imageNamed:@"chat_receiver_audio_playing_full"];
        _label.frame = CGRectMake(CGRectGetMaxX(_imageView.frame) + 10, 0, labelW, labelH);
    }
}
@end
